<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar">
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        :to="'/search'"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道列表 -->
    <!-- 
      animated 滑动的动画
      border 底边框线
      swipeable 开启左右手势滑动
     -->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated border>
      <van-tab :title="item.name" v-for="item in channelsList" :key="item.id">
        <!-- {{ item.name }}的内容 -->
        <articleList :channel="item" />
      </van-tab>
      <!-- 右侧自定义内容 -->
      <!-- 占位元素 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 右侧按钮 -->
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="icon icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->
    <!-- 频道编辑弹框 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      close-icon="close"
      position="bottom"
      :style="{ height: '100%' }"
      close-icon-position="top-left"
    >
      <channel-edit
        :user-channel="channelsList"
        @close="isChannelEditShow = false"
        :active-index.sync="active"
      ></channel-edit>
    </van-popup>
    <!-- /频道编辑弹框 -->
  </div>
</template>

<script>
import { getChannels } from "@/api/user";
import articleList from "./components/articleList";
import ChannelEdit from "./components/channelEdit.vue";
import { getItem } from "@/utils/storage";
export default {
  name: "HomeIndex",
  components: {
    articleList,
    ChannelEdit
  },
  data() {
    return {
      active: 0,
      channelsList: [],
      isChannelEditShow: false
    };
  },

  created() {
    this.loadChannels();
  },

  methods: {
    // 获取用户频道
    async loadChannels() {
      try {
        // 判断当前是不是没有登录且有本地存储数据
        if (!this.$store.state.user && getItem("USER_CHANNELS")) {
          // 直接获取本地数据
          this.channelsList = getItem("USER_CHANNELS");
        } else {
          // 发送请求获取数据
          let { data: res } = await getChannels();
          this.channelsList = res.data.channels;
        }
      } catch (error) {
        this.$toast.fail("获取用户频道失败");
      }
    }
  }
};
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 192px;
  // 深度选择器问题
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  /deep/ .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
  }
  /deep/ .van-tabs__line {
    width: 32px;
    height: 6px;
    background-color: #3296fa;
    bottom: 40px;
  }
  /deep/ .van-tab__pane {
    font-size: 40px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.icon {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/img/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
